/**
 * 文 件 名:  WJBaseQuickAdapter.java
 * 版    权:  Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  baoyinbo 00501
 * 修改时间:  2017/1/17
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */

package com.shuaiche.yeguan.base;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shuaiche.yeguan.R;


import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 列表适配器基类
 *
 */
public abstract class SCBaseQuickAdapter<T> extends BaseQuickAdapter<T> {
    private Context context;
    private TextView textView;
    private View footView;
    public SCBaseQuickAdapter(Context context, int layoutResId, List<T> data) {
        super(layoutResId,data);
        this.context=context;
        footView = LayoutInflater.from(context).inflate(R.layout.sc_list_foot_load_more, null);
        textView=(TextView)footView.findViewById(R.id.textView);

    }

    public void setFirstOnly(boolean firstOnly) {
        isFirstOnly = firstOnly;
    }

    private int mDuration = 300;
    private int mLastPosition = -1;
    private boolean isFirstOnly = false;
    private boolean isOpenAnimation = true;
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int positions) {
        super.onBindViewHolder(holder, positions);

//        if (isOpenAnimation) {
//            if (!isFirstOnly || positions > mLastPosition) {
//                BaseAnimation animation = new BaseAnimation() {
//                    @Override
//                    public Animator[] getAnimators(View view) {
//                        return new Animator[]{
//                                ObjectAnimator.ofFloat(view, "alpha", 0.5f, 1f),
//                                ObjectAnimator.ofFloat(view, "scaleY", 0.2f, 1, 1),
//                                ObjectAnimator.ofFloat(view, "scaleX", 0.2f, 1, 1)
//                        };
//                    }
//                };
//
//                for (Animator anim : animation.getAnimators(holder.itemView)) {
//                    anim.setDuration(mDuration).start();
//                }
//                mLastPosition = positions;
//            }
//        }

    }


    public SCBaseQuickAdapter(Context context, List<T> data) {
        this(context,0, data);
    }

    public SCBaseQuickAdapter(View contentView, List<T> data) {
        this(contentView.getContext(),0, data);
    }

    @Override
    public void addData(List<T> data) {
        super.addData(data);
        if(data==null)return;
        if(getData().size() >= getPageSize()){
            completeLoadMore(data.size() == getPageSize());
        }
    }

    @Override
    public void setNewData(List<T> data) {
        removeAllFooterView();
        super.setNewData(data);
    }

    public Context getContext() {
        return context;
    }

    /**
     * 完成加载更多
     */
    public void completeLoadMore(boolean isNextLoad) {
        completeLoadMore(isNextLoad, null);
    }

    /**
     * 完成加载更多
     *
     * @param title
     */
    public void completeLoadMore(boolean isNextLoad, @Nullable String title) {
        completeLoadMore(isNextLoad,title,null);
    }

    /**
     * 完成加载更多
     *
     * @param title
     */
    public void completeLoadMore(boolean isNextLoad,
                                 @Nullable String title,
                                 @Nullable View.OnClickListener clickListener) {
        if (!isNextLoad) {
            if (!TextUtils.isEmpty(title)) {
                textView.setText(title);
            }else{
                textView.setText("已经到底啦～");
            }
            if(clickListener!=null){
                footView.setClickable(true);
                footView.setOnClickListener(clickListener);
            }else{
                footView.setClickable(false);
            }
            if (getFooterLayout() != null) {
                removeFooterView(footView);
            }
            addFooterView(footView);
        }
        notifyDataChangedAfterLoadMore(isNextLoad);
    }
}
